package ccm.itesm.mx.servicios;

//import android.app.Activity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListaOpcionB extends ListActivity {
	// private String opciones [];
	private ArrayList<String> equipos, crn;
	String tipo;
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.faltas);
		Bundle bundle = this.getIntent().getExtras();
		tipo = bundle.getString("tipo");
		equipos = new ArrayList<String>();
		crn = new ArrayList<String>();
		cargaEquipos();
		this.setListAdapter(new ArrayAdapter<String>(this,
				R.layout.listactivity, equipos));

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView,
	 * android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		switch (position) {

		default:
			Bundle bunny = new Bundle();
			bunny.putString("crn", crn.get(position));
			Intent intento = new Intent(getApplicationContext(),
					MenuRepre.class);
			intento.putExtras(bunny);
			startActivity(intento);
			break;
		}
	}

	private void cargaEquipos() {
		
		String consulta = "http://modulocampusandroid.appspot.com/JsonEquipoTipo?tipo="
				+ tipo;
		String respuesta = download(consulta);

		if (respuesta != null && !respuesta.startsWith("Error")) {
			try {

				JSONArray resu = new JSONArray(respuesta);
				for (int i = 0; i < resu.length(); i++) {
					JSONObject equipolist = resu.getJSONObject(i);
					this.equipos.add(equipolist.getString("equipo"));
					this.crn.add(equipolist.getString("crn"));
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	private String download(String urlString) {

		InputStream in = null;
		byte[] data = null;
		URLConnection conn = null;
		try {
			URL url = new URL(urlString);
			conn = url.openConnection();

			if ((conn instanceof HttpURLConnection)) {
				HttpURLConnection httpConn = (HttpURLConnection) conn;
				httpConn.setAllowUserInteraction(false);
				httpConn.setInstanceFollowRedirects(true);
				httpConn.setRequestMethod("GET");
				httpConn.connect();

				if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					in = httpConn.getInputStream();
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					int c;
					while ((c = in.read()) > -1)
						baos.write(c);
					data = baos.toByteArray();
					baos.close();
					in.close();
					String str = new String(data);
					return str;
				} else {
					return ("Error reading data");
				}
			}
		} catch (Exception ex) {
			return ("Error in connection");
		} finally {
			try {
				if (conn != null) {
					conn = null;
				}
				if (in != null) {
					in.close();
					in = null;
				}

			} catch (IOException ex) {
				return ("Error: " + ex.getMessage());
			}
		}
		return null;
	}

}
